package org.yohailong.project.common.common.utils;

import org.yohailong.project.common.common.enums.CommonBizErrorEnum;
import org.yohailong.project.common.common.error.BizError;
import org.yohailong.project.common.common.error.BizException;
import org.yohailong.project.common.pojo.vo.Response;

public class ResponseUtil {

    private ResponseUtil() {
        throw new AssertionError();
    }

    public static <T> Response<T> success(T data) {
        Response<T> res = new Response<>();
        res.setSuccess(Boolean.TRUE);
        res.setData(data);
        res.setCode(CommonBizErrorEnum.SUCCESS.errorCode());
        res.setMsg(CommonBizErrorEnum.SUCCESS.errorMsg());
        return res;
    }

    public static <T> Response<T> failure(BizError bizError) {
        Response<T> res = new Response<>();
        res.setSuccess(Boolean.FALSE);
        res.setData(null);
        res.setCode(bizError.errorCode());
        res.setMsg(bizError.errorMsg());
        return res;
    }


    public static <T> Response<T> bizException(BizException exception) {
        Response<T> res = new Response<>();
        res.setSuccess(Boolean.FALSE);
        res.setData(null);
        res.setCode(exception.getErrorCode());
        res.setMsg(exception.getErrorMsg());
        return res;
    }

}
